﻿Imports System.Web
Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.Collections.Generic

<WebService(Namespace:="http://tempuri.org/")> _
<WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Public Class MembershipProvider
    Inherits System.Web.Services.WebService

#Region " Private Methods"

    Private Function GetProvider(ByVal providerName As String, ByVal applicationName As String) As System.Web.Security.MembershipProvider

        Dim provider As System.Web.Security.MembershipProvider

        If providerName IsNot Nothing AndAlso System.Web.Security.Membership.Providers(providerName) IsNot Nothing Then
            provider = System.Web.Security.Membership.Providers(providerName)
        Else
            provider = System.Web.Security.Membership.Provider
        End If

        If applicationName IsNot Nothing Then
            provider.ApplicationName = applicationName
        End If

        Return provider

    End Function

    Private Function BuildUserCollection(ByVal collection As System.Web.Security.MembershipUserCollection) As List(Of MembershipUser)

        Dim list As New List(Of MembershipUser)

        If collection IsNot Nothing Then
            For Each _membershipUser As System.Web.Security.MembershipUser In collection
                list.Add(New MembershipUser(_membershipUser))
            Next
        End If

        Return list

    End Function

#End Region

#Region " Web Methods "

    <WebMethod()> _
       Public Function ChangePassword(ByVal providerName As String, ByVal applicationName As String, ByVal username As String, ByVal oldPassword As String, ByVal newPassword As String) As Boolean
        Return GetProvider(providerName, applicationName).ChangePassword(username, oldPassword, newPassword)
    End Function

    <WebMethod()> _
    Public Function ChangePasswordQuestionAndAnswer(ByVal providerName As String, ByVal applicationName As String, ByVal username As String, ByVal password As String, ByVal newPasswordQuestion As String, ByVal newPasswordAnswer As String) As Boolean
        Return GetProvider(providerName, applicationName).ChangePasswordQuestionAndAnswer(username, password, newPasswordQuestion, newPasswordAnswer)
    End Function

    <WebMethod()> _
    Public Function CreateUser(ByVal providerName As String, ByVal applicationName As String, ByVal username As String, ByVal password As String, ByVal email As String, ByVal passwordQuestion As String, ByVal passwordAnswer As String, ByVal isApproved As Boolean, ByVal providerUserKey As Object, ByRef status As System.Web.Security.MembershipCreateStatus) As MembershipUser
        Return New MembershipUser(GetProvider(providerName, applicationName).CreateUser(username, password, email, passwordQuestion, passwordAnswer, isApproved, providerUserKey, status))
    End Function

    <WebMethod()> _
    Public Function DeleteUser(ByVal providerName As String, ByVal applicationName As String, ByVal username As String, ByVal deleteAllRelatedData As Boolean) As Boolean
        Return GetProvider(providerName, applicationName).DeleteUser(username, deleteAllRelatedData)
    End Function

    <WebMethod()> _
    Public Function FindUsersByEmail(ByVal providerName As String, ByVal applicationName As String, ByVal emailToMatch As String, ByVal pageIndex As Integer, ByVal pageSize As Integer, ByRef totalRecords As Integer) As List(Of MembershipUser)
        Return BuildUserCollection(GetProvider(providerName, applicationName).FindUsersByEmail(emailToMatch, pageIndex, pageSize, totalRecords))
    End Function

    <WebMethod()> _
    Public Function FindUsersByName(ByVal providerName As String, ByVal applicationName As String, ByVal usernameToMatch As String, ByVal pageIndex As Integer, ByVal pageSize As Integer, ByRef totalRecords As Integer) As List(Of MembershipUser)
        Return BuildUserCollection(GetProvider(providerName, applicationName).FindUsersByName(usernameToMatch, pageIndex, pageSize, totalRecords))
    End Function

    <WebMethod()> _
    Public Function GetAllUsers(ByVal providerName As String, ByVal applicationName As String, ByVal pageIndex As Integer, ByVal pageSize As Integer, ByRef totalRecords As Integer) As List(Of MembershipUser)
        Return BuildUserCollection(GetProvider(providerName, applicationName).GetAllUsers(pageIndex, pageSize, totalRecords))
    End Function

    <WebMethod()> _
    Public Function GetNumberOfUsersOnline(ByVal providerName As String, ByVal applicationName As String) As Integer
        Return GetProvider(providerName, applicationName).GetNumberOfUsersOnline()
    End Function

    <WebMethod()> _
    Public Function GetPassword(ByVal providerName As String, ByVal applicationName As String, ByVal username As String, ByVal answer As String) As String
        Return GetProvider(providerName, applicationName).GetPassword(username, answer)
    End Function

    <WebMethod()> _
    Public Function GetUserByUserKey(ByVal providerName As String, ByVal applicationName As String, ByVal providerUserKey As Object, ByVal userIsOnline As Boolean) As MembershipUser
        Return New MembershipUser(GetProvider(providerName, applicationName).GetUser(providerName, userIsOnline))
    End Function

    <WebMethod()> _
    Public Function GetUserByUsername(ByVal providerName As String, ByVal applicationName As String, ByVal username As String, _
    ByVal userIsOnline As Boolean) As MembershipUser
        Return New MembershipUser(GetProvider(providerName, applicationName).GetUser(username, userIsOnline))
    End Function

    <WebMethod()> _
    Public Function GetUserNameByEmail(ByVal providerName As String, ByVal applicationName As String, ByVal email As String) As String
        Return GetProvider(providerName, applicationName).GetUserNameByEmail(email)
    End Function

    <WebMethod()> _
    Public Function ResetPassword(ByVal providerName As String, ByVal applicationName As String, ByVal username As String, ByVal answer As String) As String
        Return GetProvider(providerName, applicationName).ResetPassword(username, answer)
    End Function

    <WebMethod()> _
    Public Function UnlockUser(ByVal providerName As String, ByVal applicationName As String, ByVal userName As String) As Boolean
        Return GetProvider(providerName, applicationName).UnlockUser(userName)
    End Function

    <WebMethod()> _
    Public Sub UpdateUser(ByVal providerName As String, ByVal applicationName As String, ByVal user As MembershipUser)
        Dim provider As System.Web.Security.MembershipProvider = GetProvider(providerName, applicationName)
        provider.UpdateUser(user.GetDefaultMembershipUser)
    End Sub

    <WebMethod()> _
    Public Function ValidateUser(ByVal providerName As String, ByVal applicationName As String, ByVal username As String, ByVal password As String) As Boolean
        Return GetProvider(providerName, applicationName).ValidateUser(username, password)
    End Function

    <WebMethod()> _
   Public Function GetProviderSettings(ByVal providerName As String, ByVal applicationName As String) As ProviderSettingCollection

        Dim providerSettingCollection As New ProviderSettingCollection
        Dim membershipProvider As System.Web.Security.MembershipProvider = GetProvider(providerName, applicationName)

        With membershipProvider
            providerSettingCollection.Add(New ProviderSetting("enablepasswordreset", .EnablePasswordReset))
            providerSettingCollection.Add(New ProviderSetting("enablepasswordretrieval", .EnablePasswordRetrieval))
            providerSettingCollection.Add(New ProviderSetting("maxinvalidpasswordattempts", .MaxInvalidPasswordAttempts))
            providerSettingCollection.Add(New ProviderSetting("minRequirednonalphanumericcharacters", .MinRequiredNonAlphanumericCharacters))
            providerSettingCollection.Add(New ProviderSetting("minrequiredpasswordlength", .MinRequiredPasswordLength))
            providerSettingCollection.Add(New ProviderSetting("passwordattemptwindow", .PasswordAttemptWindow))
            providerSettingCollection.Add(New ProviderSetting("passwordformat", .PasswordFormat))
            providerSettingCollection.Add(New ProviderSetting("enablepasswordreset", .EnablePasswordReset))
            providerSettingCollection.Add(New ProviderSetting("passwordstrengthregularexpression", .PasswordStrengthRegularExpression))
            providerSettingCollection.Add(New ProviderSetting("requiresquestionandanswer", .RequiresQuestionAndAnswer))
            providerSettingCollection.Add(New ProviderSetting("requiresuniqueemail", .RequiresUniqueEmail))
            providerSettingCollection.Add(New ProviderSetting("applicationname", .ApplicationName))
        End With

        Return providerSettingCollection

    End Function

#End Region

End Class
